// #ifdef H5
import Tools from '@/utils/Tools.js'
import jweixin from 'jweixin-module';
// #endif
const ShareH5Mixin = {
	// #ifdef H5
	data() {
		return {
			shareData: {
				share_brief: '',
				share_image: '',
				share_path: '',
				share_title: '',
				share_url: '',
				share_query: ''
			},
			actionList: [],
			appSharePopup: null,
		}
	},
	methods: {
		initH5Share(url = '', callback = null, ) {
			if (!Tools.isWechat()) {
				return false
			}
			if (!url) {
				url = location.href.split('#')[0]
			}
			this.$u.api.getWechatConfig(url).then(res => {
				const { config } = res.data
				setTimeout(() => {
					jweixin.config({
						debug: config.debug,
						appId: config.appId,
						timestamp: config.timestamp,
						nonceStr: config.nonceStr,
						signature: config.signature,
						jsApiList: config.jsApiList

					});

					jweixin.ready(() => {
						callback && callback();
						this.setWechatShare()
					});
					jweixin.error((e) => {
						console.log(e);
						const entryUrl = uni.getStorageSync('entryUrl') || '';
						if (url != entryUrl && entryUrl) {
							this.initH5Share(entryUrl, callback)
						}
					})

				}, 0)
			})
		},
		setWechatShare() {
			jweixin.updateAppMessageShareData({
				title: this.shareData.share_title, // 分享标题
				desc: this.shareData.share_brief, // 分享描述
				link: this.shareData.share_url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				imgUrl: this.shareData.share_image // 分享图标
			});
			jweixin.onMenuShareTimeline({
				title: this.shareData.share_title, // 分享标题
				link: this.shareData.share_url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				imgUrl: this.shareData.share_image // 分享图标
			});
		}

	}
	// #endif

}

export default ShareH5Mixin;
